package com.dzy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dzy.base.BaseDao;
import com.dzy.base.BaseServiceImpl;
import com.dzy.dao.PermissionDao;
import com.dzy.dao.RolePermissionDao;
import com.dzy.entity.Permission;
import com.dzy.entity.RolePermission;
import com.dzy.helper.PermissionHelper;
import com.dzy.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    protected BaseDao<Permission> getEntityDao() {
        return this.permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
        //获取所有的权限列表
        List<Permission> permissionList=this.permissionDao.findAll();
        //获取当前角色的权限id列表
        List<Long> permissionIdList=this.permissionDao.findPermissionByRoleId(roleId);
        //根据当前角色的权限id列表准备zTree的数据
        List<Map<String,Object>> zNodes=new ArrayList();
        for(Permission permission:permissionList){
            Map<String,Object> zNode=new HashMap();
            zNode.put("id",permission.getId());
            zNode.put("pId",permission.getParentId());
            zNode.put("name",permission.getName());
            if(permissionIdList.contains(permission.getId())) {
                zNode.put("checked", true);
            }
            //zNode.put("open",);//open不在后台赋值，在前台处理
            zNodes.add(zNode);
        }
        //返回结果
        return zNodes;
    }
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Override
    public void saveRolePermissionRealtionShip(Long roleId, Long[] permissionIds) {
        //删除之前的权限列表
        rolePermissionDao.deleteByRoleId(roleId);

        //添加现在新的权限列表
        if(!StringUtils.isEmpty(permissionIds)){
            for (Long permissionId:permissionIds){
                RolePermission rolePermission=new RolePermission();
                rolePermission.setPermissionId(permissionId);
                rolePermission.setRoleId(roleId);
                this.rolePermissionDao.insert(rolePermission);
            }
        }

    }

    @Override
    public List<Permission> findMenuPermissionByAdminId(Long adminId) {
        //获取用户的权限列表
        List<Permission> permissionList=null;//不带下级
        if(adminId==1){
            permissionList = this.permissionDao.findAllMenuPermission();
        }else{
            permissionList=this.permissionDao.findMenuPermissionByAdminId(adminId);
        }
        //将用户的权限列表转换为动态菜单的形式
        List<Permission> result=null;//带下级
        result= PermissionHelper.bulid(permissionList);

        return result;
    }

    @Override
    public List<Permission> findAllMenu() {
        List<Permission> permissionList=permissionDao.findAll();
        if(CollectionUtils.isEmpty(permissionList)) return null;
        List<Permission> result=PermissionHelper.bulid(permissionList);
        return result;
    }

    @Override
    public List<String> findCodeListByAdminId(Long adminId) {
        //获取用户的权限列表
        List<String> codeList=null;//不带下级
        if(adminId==1){
            codeList = this.permissionDao.findAllCodeList();
        }else{
            codeList=this.permissionDao.findCodeListByAdminId(adminId);
        }


        return codeList;
    }
}
